package com.prs.crm.action.doc;

import java.io.File;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.doc.Formwork;
import com.prs.crm.domain.doc.FormworkType;
import com.prs.crm.service.doc.FormworkService;
import com.prs.crm.service.doc.FormworkTypeService;

public class FormworkAction extends BaseAction {
	private Formwork formwork;
	private FormworkService formworkService;
	private PaginationSupport pagination;
	private Integer[] ids;

	private FormworkTypeService formworkTypeService;
	private Collection<FormworkType> type;

	private File upload;
	private String uploadFileName;
	private String formType;

	@Action("listFormwork")
	public String listFormwork() {
		return SUCCESS;
	}

	@Action("listFormworkByType")
	public String listFormworkByType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Formwork.class);
		criteria.createAlias("type", "t");
		if (formType != null) {
			if (!"all".equals(formType)) {
				formType = formType.split(",")[0];
				if (ToolKits.isInteger(formType)) {
					criteria.add(Restrictions.eq("t.id", Integer
							.parseInt(formType)));
				}
			}
		}
		criteria.addOrder(Order.asc("t.name"));
		criteria.addOrder(Order.desc("recordTime"));

		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFormworkService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editFormwork")
	public String editFormwork() {
		type = this.getFormworkTypeService().getAllFormworkType();
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "submitFormwork", results = {
			@Result(name = "success", type = "redirect", location = "editFormwork"),
			@Result(name = "error", type = "chain", location = "editFormwork"),
			@Result(name = "none", type = "redirect", location = "alterFormwork") })
	public String submitFormwork() {
		if (formwork.getName() == null || "".equals(formwork.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (formwork.getType() == null) {
			addActionError("类型不能为空");
			return ERROR;
		}
		if (formwork.getId() != null) {
			this.getFormworkService().saveFormwork(formwork, upload,
					uploadFileName);
			return NONE;
		}
		this.getFormworkService()
				.saveFormwork(formwork, upload, uploadFileName);
		this.setPrompt("提交模板成功");
		return SUCCESS;
	}

	@Action("viewFormwork")
	public String viewFormwork() {
		return SUCCESS;
	}

	@Action("alterFormwork")
	public String alterFormwork() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Formwork.class);
		criteria.createAlias("type", "t");
		criteria.addOrder(Order.asc("t.name"));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFormworkService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("alterFormworkByType")
	public String alterFormworkByType() {
		return this.listFormworkByType();
	}

	@Action(value = "removeFormwork", results = {
			@Result(name = "success", type = "redirect", location = "alterFormwork"),
			@Result(name = "error", type = "chain", location = "alterFormwork") })
	public String removeFormwork() {
		if (ids == null) {
			this.addActionError("请选择要删除的模板");
			return ERROR;
		}
		this.getFormworkService().batchRemove(ids);
		return SUCCESS;
	}

	@Action("selectDocTemplate")
	public String selectDocTemplate() {
		return this.alterFormwork();
	}

	public Formwork getFormwork() {
		return formwork;
	}

	public void setFormwork(Formwork formwork) {
		this.formwork = formwork;
	}

	public FormworkService getFormworkService() {
		return formworkService;
	}

	public void setFormworkService(FormworkService formworkService) {
		this.formworkService = formworkService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public FormworkTypeService getFormworkTypeService() {
		return formworkTypeService;
	}

	public void setFormworkTypeService(FormworkTypeService formworkTypeService) {
		this.formworkTypeService = formworkTypeService;
	}

	public Collection<FormworkType> getType() {
		return type;
	}

	public void setType(Collection<FormworkType> type) {
		this.type = type;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getFormType() {
		return formType;
	}

	public void setFormType(String formType) {
		this.formType = formType;
	}

}
